package backend;


import java.util.ArrayList;
import java.util.List;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class ProductsCat {
	private List<Product> products = new ArrayList<Product>();
	private String url="http://eiffel.itba.edu.ar/hci/service/Catalog.groovy?method=GetProductListByCategory&language_id=1&category_id=1";

	public void initialize() {
		try{
			Connect con = new Connect();
			Document dom = con.getConection(url);
			NodeList prods = dom.getElementsByTagName("product");
			Product prod = null;
			Node node = null;
			NodeList att = null;
			String prodID=null, catID=null, subCatID=null, rank=null, price=null, name=null, url=null;
			for(int i=0; i<prods.getLength(); i++){
				node = prods.item(i);
				att = node.getChildNodes();
								
				prodID=node.getAttributes().item(0).getNodeValue();
				for(int j=0; j<att.getLength(); j++){
					if(att.item(j).getNodeName().equals("category_id"))
						catID=att.item(j).getTextContent();
					else if(att.item(j).getNodeName().equals("subcategory_id"))
						subCatID=att.item(j).getTextContent();
					else if(att.item(j).getNodeName().equals("name"))
						name=att.item(j).getTextContent();
					else if(att.item(j).getNodeName().equals("sales_rank"))
						rank=att.item(j).getTextContent();
					else if(att.item(j).getNodeName().equals("price"))
						price=att.item(j).getTextContent();
					else if(att.item(j).getNodeName().equals("image_url"))
						url=att.item(j).getTextContent();
				}
				
				prod = new Product(prodID, catID, subCatID, name, rank, price, url);
				
				this.products.add(prod);	
			}
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	public List<Product> getProducts(){
		return this.products;
	}
	
}
